package com.yuanyu.server.impl;

import com.yuanyu.mapper.mysqlMapper.MainTable_Mapper;
import com.yuanyu.mapper.oracleMapper.OracleTestMapper;
import com.yuanyu.model.entity.MainTable;
import com.yuanyu.model.entity.OracleWorkOrder;
import com.yuanyu.server.IScheduledTasks;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;


@Slf4j
@Component
@EnableScheduling
@EnableAsync
public class ScheduledTasksImpl implements IScheduledTasks {

    @Autowired
    private MainTable_Mapper mainMapper;
    @Autowired
    private OracleTestMapper oracleTestMapper;

    @Override
    @Async("TaskExecutor")
//    @Scheduled(cron = "0 0/10 6-20 * * MON-FRI")
    @Scheduled(cron = "*/10 * * * * ?")
    public void test() {
        MainTable mainTable = mainMapper.selByCase("w123123");
        System.out.println("mainTable = " + mainTable);
        List<OracleWorkOrder> oracleWorkOrders = oracleTestMapper.oracleTestCount();
        for (OracleWorkOrder oracleWorkOrder : oracleWorkOrders) {
            System.out.println("oracleWorkOrder = " + oracleWorkOrder);
            MainTable test = mainMapper.selByCase(oracleWorkOrder.getSfaadocno());
            if (test == null){
                // 将Date转换为LocalDateTime，这里使用了系统默认时区
                LocalDateTime localDateTime = oracleWorkOrder.getSfaadocdt().toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDateTime();

                // 打印转换后的LocalDateTime
                MainTable mainTable1 = new MainTable(
                        oracleWorkOrder.getSfaadocno(),
                        oracleWorkOrder.getSfaa010(),
                        oracleWorkOrder.getImaal003(),
                        oracleWorkOrder.getXmdc030(),
                        oracleWorkOrder.getSfaadocno(),
                        oracleWorkOrder.getPmaal004(),
                        localDateTime,
                        Integer.valueOf(oracleWorkOrder.getSfaa012()),
                        0,
                        "-1",
                        null,
                        null,
                        true,
                        true,
                        true,
                        true,
                        true,
                        true,
                        true,
                        true,
                        true,
                        true,
                        true
                );
                mainMapper.addMainTable(mainTable1);
                log.info("该工单\"{}\"已添加成功,值为 : {}",oracleWorkOrder.getSfaadocno(),mainTable1);
            }else {
                log.info("该工单\"{}\"已存在,值为 : {}",oracleWorkOrder.getSfaadocno(),test);
            }
        }
    }
}
